package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.Xdfeizhi;
import com.amsp.oa.oademo.entity.XdfeizhiRevision;
import com.amsp.oa.oademo.mapper.XdfeizhiMapper;
import com.amsp.oa.oademo.mapper.XdfeizhiRevisionMapper;
import com.amsp.oa.oademo.service.XdfeizhiRevisionService;
import com.amsp.oa.oademo.service.XdfeizhiService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service
public class XdfeizhiServiceImpl extends ServiceImpl<XdfeizhiMapper, Xdfeizhi> implements XdfeizhiService {

    @Resource
    private XdfeizhiRevisionService xdfeizhiRevisionService;

    @Override
    public IPage<Xdfeizhi> getXdfeizhiPage(Integer pageNum, Integer pageSize, String keyword) {
        Page<Xdfeizhi> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Xdfeizhi> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.hasText(keyword)) {
            wrapper.like(Xdfeizhi::getFileName, keyword)
                    .or()
                    .like(Xdfeizhi::getFileNumber, keyword)
                    .or()
                    .like(Xdfeizhi::getApplicationDepartment, keyword);
        }

        wrapper.orderByDesc(Xdfeizhi::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    public Xdfeizhi getXdfeizhiWithRevisions(Long id) {
        Xdfeizhi xdfeizhi = this.getById(id);
        if (xdfeizhi != null) {
            LambdaQueryWrapper<XdfeizhiRevision> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(XdfeizhiRevision::getXdfeizhiId, id);
            xdfeizhi.setRevisions(xdfeizhiRevisionService.list(wrapper));
        }
        return xdfeizhi;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveXdfeizhiWithRevisions(Xdfeizhi xdfeizhi) {
        boolean result = this.saveOrUpdate(xdfeizhi);
        if (result && xdfeizhi.getRevisions() != null) {
            //revisionMapper
            xdfeizhi.getRevisions().stream().forEach(d->{
                d.setXdfeizhiId(xdfeizhi.getId());
            });
            xdfeizhiRevisionService.saveOrUpdateBatch(xdfeizhi.getRevisions());
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateXdfeizhiWithRevisions(Xdfeizhi xdfeizhi) {
        boolean result = this.updateById(xdfeizhi);
        if (result && xdfeizhi.getRevisions() != null) {
            // 删除原有的修订记录
            LambdaQueryWrapper<XdfeizhiRevision> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(XdfeizhiRevision::getXdfeizhiId, xdfeizhi.getId());
            xdfeizhiRevisionService.remove(wrapper);

            // 保存新的修订记录
            for (XdfeizhiRevision revision : xdfeizhi.getRevisions()) {
                revision.setXdfeizhiId(xdfeizhi.getId());
                xdfeizhiRevisionService.save(revision);
            }
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeXdfeizhiWithRevisions(Long id) {
        // 删除修订记录
        LambdaQueryWrapper<XdfeizhiRevision> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(XdfeizhiRevision::getXdfeizhiId, id);
        xdfeizhiRevisionService.remove(wrapper);

        // 删除主记录
        return this.removeById(id);
    }
}